package com.app.megaprosoft.DB;


import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ItemUnit {
	
	Connect con = new Connect(); 
	 Connection conn = con.Connect();
	 ResultSet rs = null;
	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 Context context;
	 private static final String TABLE_ItemUnit = "_ItemUnit";
	 public Context getContext() {
		return context;
	 }

	 public void setContext(Context context) {
		this.context = context;
	 }
	 
	private String OID,
		    UsrOpt,
		    IsActive,
		    IsDeleted,
			ItemUnitCode,
			ItemUnitDesc;
    
    public ItemUnit() {ItemUnitCode=ItemUnitDesc="";
    }
	
    public void closeConnecttion() throws Exception {
   	 if (conn != null) {
            conn.close();
        }
   }
    
	public ItemUnit(
			String OID,
    		String UsrOpt,
    		String IsActive,
    		String IsDeleted,
			String ItemUnitCode,
			String ItemUnitDesc
	)
	{
		this.OID = OID;
    	this.UsrOpt = UsrOpt;
    	this.IsActive = IsActive;
    	this.IsDeleted = IsDeleted;
		this.ItemUnitCode = ItemUnitCode;
		this.ItemUnitDesc = ItemUnitDesc;
	}
	
	public String getOID() {
		return OID;
	}

	public void setOID(String oID) {
		OID = oID;
	}

	public String getUsrOpt() {
		return UsrOpt;
	}

	public void setUsrOpt(String usrOpt) {
		UsrOpt = usrOpt;
	}

	public String getIsActive() {
		return IsActive;
	}

	public void setIsActive(String isActive) {
		IsActive = isActive;
	}

	public String getIsDeleted() {
		return IsDeleted;
	}

	public void setIsDeleted(String isDeleted) {
		IsDeleted = isDeleted;
	}
	
    public void setItemUnitCode(String ItemUnitCode)
	{
		this.ItemUnitCode = ItemUnitCode;
	}
	public void setItemUnitDesc(String ItemUnitDesc)
	{
		this.ItemUnitDesc = ItemUnitDesc;
	}
	
	public String getItemUnitCode()
	{
		return this.ItemUnitCode;
	}
	public String getItemUnitDesc()
	{
		return this.ItemUnitDesc;
	}
	
	public ResultSet selectItemUnit(String StrItemUnitDesc) throws Exception {
		String StrSql   = "SELECT [OID]," +
				"[LastModified]," +
				"[ModifyDate]," +
				"[ModifyTime]," +
				"[ModifiedBy]," +
				"[UserModify]," +
				"[CreatedDate]," +
				"[CreateDate]," +
				"[CreateTime]," +
				"[CreatedBy]," +
				"[UserCreate]," +
				"[IsActive]," +
				"[IsDeleted]," +	
				
        		"[ItemUnitCode]," +
        		"[ItemUnitDesc] " +
        		
        		"FROM [dbo].[udfItemUnit] () " +
        		"WHERE [IsDeleted]='0' ";   	
		
		if (!StrItemUnitDesc.equals("")) {
            StrSql += "AND [ItemUnitDesc] LIKE '%" + StrItemUnitDesc + "%'";
        }
    	
		return con.selectData(StrSql);
	 }
	
	public ResultSet defineItemUnit() throws Exception {
        String StrSql = "SELECT [OID]," +
        		"[ItemUnitCode]"+
				",[ItemUnitDesc] "+		
               
                "FROM [dbo].[udfItemUnit] ()";

        StrSql += "ORDER BY [ItemUnitDesc] ASC";

        return con.selectData(StrSql);
    }
	
	public void DEFINE_ItemUnit() {
		rs = null;
		
		myDb = new SQLiteDBClass(getContext());
		db = myDb.getWritableDatabase();

		try {
			rs = selectItemUnit("");
			while (rs.next()) {
				ContentValues Val = new ContentValues();
				Val.put("ItemUnitOID", rs.getString("OID"));

				Val.put("ItemUnitCode", rs.getString("ItemUnitCode"));
				Val.put("ItemUnitDesc", rs.getString("ItemUnitDesc"));

				db.insert(TABLE_ItemUnit, null, Val);
			}
		} catch (Exception e) {
			Log.d("TABLE_ItemUnit", "Create Table not Successfully.");
		}finally{
			try {
				closeConnecttion();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Log.d("TABLE_ItemUnit", "Create Table Successfully.");
		db.close();
	}

	
	
}
